// index.js
// 获取应用实例
const app = getApp()
const udp = wx.createUDPSocket()

Page({
    data: {
        inputValue: '',
    },
    //编辑框不为空时  增加mess 标识符
    input1(e) {
        this.setData({
            inputValue: e.detail.value
          })

          if (this.data.inputValue != "") {
            app.globalData.mess= "&user:"+ this.data.inputValue  
            console.log(app.globalData.mess)
        } 

    },
    onShareAppMessage() {
        return {
            title: 'UDPS3',
            path: res.webViewUrl,
            imageUrl: 'https://dabai-1259760387.cos.ap-beijing.myqcloud.com/801%E5%B7%A5%E5%85%B7%E7%AE%B1/img/801-icon-200.jpg',
        }
    },
    onShareTimeline() {
        return {
            title: 'UDPS3',
            query: '',
            imageUrl: 'https://dabai-1259760387.cos.ap-beijing.myqcloud.com/801%E5%B7%A5%E5%85%B7%E7%AE%B1/img/801-icon-200.jpg',
        }
    },
    onAddToFavorites(res) {
        // webview 页面返回 webViewUrl
        console.log('webViewUrl: ', res.webViewUrl)
        return {
            title: 'UDPS3',
            imageUrl: 'https://dabai-1259760387.cos.ap-beijing.myqcloud.com/801%E5%B7%A5%E5%85%B7%E7%AE%B1/img/801-icon-200.jpg',
            query: '',
        }
    },
    bindViewTap1(e) {
        wx.showModal({
            title: '提示',
            content: "确认关闭电脑嘛？",
            success(res) {
                if (res.confirm) {
                    console.log('确定')
                    udp.bind(8844)
                    udp.send({
                        address: '255.255.255.255',
                        port: 8846,
                        message: 'shutdown:l'+app.globalData.mess
                    })
                } else if (res.cancel) {
                    console.log('取消')
                }
            }
        })
    },
    bindViewTap2(e) {

        wx.showModal({
            title: '提示',
            content: "确认重启电脑嘛？",
            success(res) {
                if (res.confirm) {
                    console.log('确定')
                    udp.bind(8844)
                    udp.send({
                        address: '255.255.255.255',
                        port: 8846,
                        message: 'shutdown:r'+app.globalData.mess
                    })
                } else if (res.cancel) {
                    console.log('取消')
                }
            }
        })
    },
    bindViewTap3(e) {

        wx.showModal({
            title: '提示',
            content: "确认关闭电脑中的程序嘛？",
            success(res) {
                if (res.confirm) {
                    console.log('shutdown:this'+app.globalData.mess)

                    udp.bind(8844)
                    udp.send({
                        address: '255.255.255.255',
                        port: 8846,
                        message: 'shutdown:this'+app.globalData.mess
                    })

                } else if (res.cancel) {
                    console.log('取消')
                }
            }
        })

    },
    bindViewTap4(e) {
        wx.setClipboardData({
            data: 'https://gitee.com/dabai2017/udp-shutdown.git',
            success(res) {
                wx.getClipboardData({
                    success(res) {
                        console.log(res.data) // data
                    }
                })
            }
        })
        this.showToast2("其他终端下载链接已复制")
    },
    helpDia(e) {

        this.helpDialog()
    },
    helpDialog() {
        wx.showModal({
            title: '帮助',
            content: "请配合电脑端一起使用，点击UDPS3即可复制下载地址。\n输入自定义标识即可指定计算机，无标识即全局发送，详情请看网址说明",
            success(res) {
                if (res.confirm) {} else if (res.cancel) {}
            }
        })
    },
    showToast2(content) {
        wx.showModal({
            title: '提示',
            content: content,
            success(res) {
                if (res.confirm) {
                    console.log('确定')
                } else if (res.cancel) {
                    console.log('取消')
                }
            }
        })
    },
    udpSend(content) {
        udp.bind(8844)
        udp.send({
            address: '255.255.255.255',
            port: 8846,
            message: content
        })
    }
})